<template>
  <div class="channel-edit">
    <!-- 弹出层 频道编辑组件 -->
    <!-- 我的频道 -->
    <van-cell>
      <div slot="title">我的频道</div>
      <van-button class="edit-btn" round plain type="danger" size="mini"
        @click="isEdit=!isEdit">{{isEdit? '完成' : '编辑' }}
      </van-button>
    </van-cell>
    <van-grid class='my-grid' :gutter="10">
      <van-grid-item class="grid-item" v-for="(channel,index) in myChannels"
        :key="channel.id" :text="channel.name"
        @click="onMyChannelClick(channel,index)">
        <!-- 删除图标 -->
        <van-icon v-show="isEdit && !fiexChannels.includes(channel.id)"
          slot="icon" name="clear" />
        <span slot="text" class="text"
          :class="{active: index===active}">{{ channel.name }}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell>
      <div slot="title">频道推荐</div>
    </van-cell>
    <van-grid class='recommend-grid' :gutter="10">
      <van-grid-item class="grid-item"
        v-for="(channel,index) in recommendChannels" :key="index" icon="plus"
        :text="channel.name" @click="onAddChannel(channel)" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channe'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'

export default {
  // 组件名称
  name: 'ChannelEdit',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  // 组件状态值
  data () {
    return {
      allChannels: [], // 所有的频道
      isEdit: false, // 控制编辑状态
      fiexChannels: [0] // 固定数组,不允许删除
    }
  },
  // 计算属性
  computed: {
    recommendChannels () { // 推荐频道
      const channels = []
      this.allChannels.forEach(channel => {
        const ret = this.myChannels.find(myChannel => {
          return myChannel.id === channel.id
        })
        if (!ret) {
          channels.push(channel)
        }
      })
      return channels
    },
    ...mapState(['user'])
  },
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  created () { this.loadAllChannels() },
  mounted () { },
  // 组件方法
  methods: {
    async loadAllChannels () { // 获取全部的频道
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        console.log('失败', err)
      }
    },
    async onAddChannel (channel) { // 添加到我的频道
      this.myChannels.push(channel)
      if (this.user) {
        // 已登录
        try {
          await addUserChannel({
            id: channel.id, // 频道id
            seq: this.myChannels.length // 序号
          })
          this.$toast('添加成功')
        } catch (err) {
          this.$toast('保存失败')
        }
      } else {
        // 未登录
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    onMyChannelClick (channel, index) { // 删除||跳转
      // 固定不能删除
      if (this.fiexChannels.includes(channel.id)) return
      if (this.isEdit) {
        // 编辑状态,执行删除
        if (this.active >= index) {
          this.$emit('updata_active', this.active - 1, true)
        }
        this.myChannels.splice(index, 1)
        this.deleteChannel(channel) // 调用删除频道方法
      } else {
        // 非编辑状态, 执行跳转
        this.$emit('updata_active', index, false)
      }
    },
    async deleteChannel (channel) { // 删除频道
      try {
        if (this.user) {
          await deleteUserChannel(channel.id)
        } else {
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
        this.$toast('删除成功')
      } catch (err) {
        this.$toast('删除失败,请稍后重试')
      }
    }
  }
}

</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap; // 不换行
      background-color: #f4f5f6;
      .van-grid-item__icon-wrapper {
        position: unset;
      }
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
      }
      .active {
        color: red;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
        .van-grid-item__text {
          margin-top: 0;
        }
      }
    }
  }
}
</style>
